package com.backend.demo.dao;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.backend.demo.baseDAO.BaseDao;
import com.backend.demo.entities.Users;
import com.backend.demo.page.PageBean;
import com.backend.demo.util.DateTimeUtil;
import com.backend.demo.util.StringUtils;
import com.backend.demo.util.UserPasswordUtils;

/**
 * user的DAO
 * 
 * @author Administrator
 * 
 */
@SuppressWarnings("unchecked")
@Repository
public class UserDao extends BaseDao<Users, Long> {

	/**
	 * 查询所有用户
	 * @return
	 */
	public List<Users> getAllUsers(){
		String hql = "from Users";
		String countHql = "select count(userID) from Users";
		PageBean<Users> pageResults = findPageByFetchedHql(hql, countHql, 1, 5);
		List<Users> users = pageResults.getResutls();
		return users;
	}
	/**
	 * 登陆
	 * 
	 * @param userName
	 * @param userPassword
	 * @return
	 */
	public Users userLogin(String userName, String userPassword) {
		if(StringUtils.isEmpty(userName) || StringUtils.isEmpty(userPassword)){
			return null;
		}
		String hql = "from Users where userName=? and userPassword=?";
		userPassword = UserPasswordUtils.getPassword(userName, userPassword,false);
		Users user = (Users) getOneByHQL(hql, userName,userPassword);
		if(user != null && 0 == user.getIsActivity()){
			String Hql = "update Users set loginStatus = ? where userName=?";
			updateOrDeleteByHql(Hql, "0",user.getUserName());
			return user;
		}
		return null;
	}

	/**
	 * 注册的时候验证用户名或者昵称是否已经注册过
	 * 
	 * @param userName
	 * @param nickname
	 * @return
	 */
	public boolean validateUserNameIsUnique(String userName, String nickname) {
		String hql = "from Users where 1=1 ";
		StringBuffer buffer = new StringBuffer(hql);
		if (StringUtils.isNotEmpty(userName)) {
			buffer.append(" and userName = '" + userName+"'");
		}
		if (StringUtils.isNotEmpty(nickname)) {
			buffer.append(" and nickname = '" + nickname+"'");
		}
		Users user = getOneByHQL(buffer.toString());
		return user == null ? false : true;
	}

	/**
	 * 退出
	 */
	public int logout(String loginStatus, Long userID) {
		String hql = "update Users set loginStatus = ?,updatetime=? where userID = ?";
		return updateOrDeleteByHql(hql, loginStatus ,new Date(), userID );
	}
	
	public Users userIsLogout(String userName){
		String hql = "from Users where userName = ?";
		Users user = getOneByHQL(hql, userName);
		if(user != null && "1".equals(user.getLoginStatus())){
			return user;
		}
		return null;
	}

	/**
	 * 修改用户信息
	 * 
	 * @param user
	 */
	public int editUserMsg(Users user) {
		user.setUpdatetime(new Date());
		String hql = "update Users set nickname=?,email=?,phone=?,updatetime=?,imageFile = ? where userID=?";
		return updateOrDeleteByHql(hql, user.getNickname(),user.getEmail(),user.getPhone(),new Date(),user.getImageFile(),user.getUserID());
	}

	/**
	 * 根据用户ID删除一个或多个用户
	 * 
	 * @param ids
	 */
	public void deleteBatch(Long[] ids) {
		deleteBatch(ids);
	}
	
	public int deleteById(Long id){
		String hql = "delete from Users where userID = ?";
		return updateOrDeleteByHql(hql,id);
	}

	/**
	 * 修改密码前进行密码验证
	 * 
	 * @param userName
	 * @param password
	 * @return
	 */
	public boolean validatePassword(String userName, String password) {
		String hql = "from Users where userName = ?";
		Users users = (Users) getListByHQL(hql,userName,password);
		if (users != null) {
			password = UserPasswordUtils.getPassword(userName, password, false);
			if (password.equals(users.getUserPassword())) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}


	/**
	 * 修改密码
	 * 
	 * @param password
	 * @param modifiedPassword
	 */
	public int modifyPassword(String userName,String modifiedPassword) {
		String hql = "update Users set userPassword = ?,updatetime=? where userName = ?";
		modifiedPassword = UserPasswordUtils.getPassword(userName,modifiedPassword, false);
		return updateOrDeleteByHql(hql, modifiedPassword,new Date(),userName);
	}

	/**
	 * 添加一个用户
	 * 
	 * @param user
	 */
	public int addUsers(Users user) {
		user.setUserType(user.getRoleID().toString());
		user.setCreatetime(new Date());
		user.setUpdatetime(new Date());
		String userPassword = UserPasswordUtils.getPassword(user.getUserName(), user.getUserPassword(), false);
		user.setUserPassword(userPassword);
		return save(user);
	}

	/**
	 * 根据条件查询 条件:
	 * 用户帐号,昵称,性别,邮箱,电话,创建时间
	 * 是否可用,登录状态 综合查询,支持模糊查询
	 * @param user
	 * @return
	 */
	public List<Users> getUserByCondition(Users user) {
		StringBuffer stringBuffer = new StringBuffer("from Users where 1=1 ");
		if (user != null) {
			if(StringUtils.isNotEmpty(user.getUserName())){
				stringBuffer.append("and userName like '%"+user.getUserName()+"%'");
			}
			if(StringUtils.isNotEmpty(user.getNickname())){
				stringBuffer.append("and nickname like '%"+user.getNickname()+"%'");
			}
			if(StringUtils.isNotEmpty(user.getSex()) && !"-1".equals(user.getSex())){
				stringBuffer.append("and sex like '%"+user.getSex()+"%'");
			}
			if(StringUtils.isNotEmpty(user.getEmail())){
				stringBuffer.append("and email like '%"+user.getEmail()+"%'");
			}
			if(StringUtils.isNotEmpty(user.getPhone())){
				stringBuffer.append("and phone like '%"+user.getPhone()+"%'");
			}
			if(user.getCreatetime() != null){
				String format = DateTimeUtil.format("yyyy-MM-dd HH:mm:ss", user.getCreatetime());
				stringBuffer.append("and createtime >='"+format+"'");
			}	
		}
		List<Users> lists = getListByHQL(stringBuffer.toString());
		return lists;
	}
	
	/**
	 * 根据用户ID查询用户
	 * @param userID
	 * @return
	 */
	public Users getUsersById(long userID){
		String hql = "from Users where userID = ?";
		return getOneByHQL(hql, userID);
	}
}
